package cn.ekuma.epos.crm.link;

import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.erp.link.AbstractOrderLinkPanel;
import cn.ekuma.epos.qkorder.JOrderViewDialog;

import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBF;
import com.openbravo.data.loader.query.QBFCompareEnum;

import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.panels.event.CustomerChangedListener;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;

public class JCustomerOrderLinkPanel extends AbstractOrderLinkPanel implements
		CustomerChangedListener {
	Object[] afilter;
	AppView m_App;
	JOrderViewDialog orderViewDialog;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	I_DataLogicERP dlSales;
    public JCustomerOrderLinkPanel(AppView m_App,I_DataLogicERP dlSales){
    	super();
    	this.dlSales=dlSales;
    	this.m_App=m_App;
    }
	
	@Override
	public void valueSelectChanged(CustomerInfo obj) {
		if(obj!=null&&isIDChanged(obj.getId())){
			if(afilter==null)
				try {
					afilter=createValue();
				} catch (BasicException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ID=obj.getId();
		   afilter[12] = QBFCompareEnum.COMP_EQUALS;
           afilter[13] = obj.getName();
           query();
		}
	}

	@Override
	public  void timeCycleChanged(TimeCycleChangedEvent e) {
		if(afilter==null)
			return;
		  afilter[6] = (e.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS;
	      afilter[7] = e.getStartDate();
	      afilter[8] = ( e.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS;
	      afilter[9] = e.getEndDate();
	      query();
	}

	String[] fields= new String[]{"T.ORDERID", "T.DEFINETYPEREF", "T.ORDERTYPE", "T.CURDATE", "T.CURDATE", "P.NAME", "C.NAME", "T.LOCATIONREF", "T.TOTALPRICE"};
	public void query(){
		if(afilter==null)
			return;
		QBF qbf=QBF.c();
		for(int i=0;i<afilter.length;i=i+2)
			if(afilter[i]!=QBFCompareEnum.COMP_NONE)
				qbf.and(new QBFParameter(fields[i/2],(QBFCompareEnum) afilter[i],afilter[i+1]));
		try {
			tableModel.setData(dlSales.query(FindOrdersInfo.class,qbf.build()));
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}
	
	public static String getTabName(){
		return AppLocal.getIntString("label.order");
	}

	@Override
	public void fireOrderInfoChangedListener(FindOrdersInfo orderInfo) {
	  if(orderViewDialog==null)
		  orderViewDialog=JOrderViewDialog.getDialog(this, m_App, dlSales);
	  Order order = null;
      try {
          order = dlSales.loadOrder(orderInfo.getOrderid());
      } catch (BasicException ex) {
           MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotLoadOrder"),ex);
           msg.show(this);
           order=null;
      }
      if (order != null) {
	     orderViewDialog.setActiveOrder(order);
	     orderViewDialog.setVisible(true);
      }
		
	}
	
}
